package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.service.FilterChain;
import com.qf.smsplatform.strategy.service.api.CacheService;
import com.qf.smsplatform.strategy.util.PushMessageUtil;
import com.qf.smsplatform.strategy.wordfilter.SensitivewordFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Set;

/**
 * 作者：郝国栋
 * 项目名：IntelliJ IDEA
 * 时间:2020/8/26  16:12
 * 描述:敏感词策略
 */
@Service(value = "dirtyWords")
@Slf4j
public class DirtyWordsStrategy implements FilterChain {


    @Autowired
    private CacheService cacheService;

    @Autowired
    private PushMessageUtil pushMessage;

    @Autowired
    private SensitivewordFilter filter;


    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【敏感词策略】 敏感词策略执行了....");

        //1. 从submit中获取短信内容
        String content = submit.getMessageContent();

       /* //2. 使用IK分词器对短信内容分词
        //3. 封装成一个集合
        // 声明存储分词内容的Set集合
        Set<String> contents = new HashSet<>();
        // google出来的IK分词器使用方式,
        StringReader sr = new StringReader(content);
        IKSegmenter ik = new IKSegmenter(sr, true);
        Lexeme lex=null;
        while(true){
            try {
                if (!((lex=ik.next())!=null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            contents.add(lex.getLexemeText());
        }
        //4. 将集合添加到Redis的Set中
        cacheService.sadd(CacheConstants.CACHE_PREFIX_DIRTY_WORDS + submit.getClientID(),contents.toArray(new String[]{}));

        //5. 调用cacheService,执行交集操作,获取结果
        Set<Object> set = cacheService.intersect(CacheConstants.CACHE_PREFIX_DIRTY_WORDS + "WORDS", CacheConstants.CACHE_PREFIX_DIRTY_WORDS + submit.getClientID());

        //5.5 TODO 删除存储到set集合中的key

        //6. 判断结果的长度
        if(set.size() == 0) {
            //6.1 如果长度 == 0,没有敏感词,返回true
            log.info("【敏感词策略】 敏感词策略校验通过!!!");
            return true;
        }else {
            //6.2 如果长度 > 0,有敏感词,发送消息,返回false
            log.info("【敏感词策略】 敏感词策略校验失败!!!");
            pushMessage.pushLog(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            pushMessage.pushReport(submit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return false;
        }*/
        // TODO 当WebMaster针对敏感词进行增删改后,先数据同步到了Redis缓存.
        // TODO 让WebMaster再发送一个消息,通知策略模块,敏感词更新了.
        // TODO 让Strategy模块监听这个消息,然后让SensitivewordFilter直行initKeyWord方法,即可同步到Spring容器管理的这个单例的filter对象中

        int count = filter.checkSensitiveWord(content,0,SensitivewordFilter.minMatchType);
        // 判断敏感词长度
        if (count==0){
            //6.1 如果长度 == 0,没有敏感词,返回true
            log.info("【敏感词策略】 敏感词策略校验通过!!!");
            return true;
        }else {
            //6.2 如果长度 > 0,有敏感词,发送消息,返回false
            log.info("【敏感词策略】 敏感词策略校验失败!!!");
            pushMessage.pushLog(submit,StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            pushMessage.pushReport(submit,StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            return false;
        }
    }
}
